package Dao;

import object.Message;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MessageDaoImpt implements MessageDao {

    @Override
    public int AddMessage(Message message) {
        return useSQL.Add("message", "null", message.getTitle(), message.getAuthor(), message.getContent());
    }

    @Override
    public int DeleteMessage(Message message) {
        return useSQL.Delete("message", "mid", String.valueOf(message.getMid()));
    }

    @Override
    public int UpdateMessage(Message message) {
        return useSQL.Updeat("message", "mid", String.valueOf(message.getMid()), "title", "author", "content", message.getTitle(), message.getAuthor(), message.getContent());
    }

    @Override
    public Message SelectMessage(int mid) {
        Message message = new Message();
        Connection conn = null;
        try {
            //连接数据库，获得连接对象
            conn = BaseDao.getConnection();
            Statement statement = conn.createStatement();
            //执行sql的查询语句，得到结果集
            ResultSet reusult = statement.executeQuery("select * from message where mid = " + mid + " ; ");
            if (reusult.next()) {
                message.updateMid(reusult.getInt("mid"));
                message.updateTitle(reusult.getString("title"));
                message.updateAuthor(reusult.getString("author"));
                message.updateContent(reusult.getString("content"));
                return message;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
